package com.ganbo.redpacket.service.impl;

import com.ganbo.redpacket.entity.RedDetail;
import com.ganbo.redpacket.entity.RedRecord;
import com.ganbo.redpacket.entity.RedRobRecord;
import com.ganbo.redpacket.mapper.RedDetailMapper;
import com.ganbo.redpacket.mapper.RedRecordMapper;
import com.ganbo.redpacket.mapper.RedRobRecordMapper;
import com.ganbo.redpacket.model.requset.HandOutReq;
import com.ganbo.redpacket.service.RedPacketAsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 红包异步处理逻辑
 * Created by gan on 2020/1/12 15:34.
 */

@Service
public class RedPacketAsyncServiceImpl implements RedPacketAsyncService {

    @Autowired
    RedRecordMapper redRecordMapper;
    @Autowired
    RedDetailMapper redDetailMapper;
    @Autowired
    RedRobRecordMapper redRobRecordMapper;


    @Override
    public void recordRedPacket(HandOutReq req, String redId, List<Integer> list) {
        RedRecord redRecord = new RedRecord();
        redRecord.setUserId(req.getUserId());
        redRecord.setRedPacket(redId);
        redRecord.setTotal(req.getTotal());
        redRecord.setAmount(BigDecimal.valueOf(req.getAmount()));
        redRecordMapper.insert(redRecord);

        RedDetail detail;
        for (Integer i : list) {
            detail = new RedDetail();
            detail.setRecordId(redRecord.getId());
            detail.setAmount(BigDecimal.valueOf(i));
            redDetailMapper.insert(detail);
        }
    }

    @Override
    public void recordRodRedPacket(RedRobRecord redRobRecord) {
        redRobRecordMapper.insert(redRobRecord);
    }
}
